//
//  GameCollectionViewCell.m
//  SnailPost
//
//  Created by Q on 16/3/12.
//  Copyright © 2016年 抠泥ser. All rights reserved.
//

#import "GameCollectionViewCell.h"
#import "HeaderTableViewCell.h"
#import "NormalTableViewCell.h"
#import "ThreePicturesTableViewCell.h"
#import "NewsModel.h"
#import "LHAFNetworkTool.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#define CHEIGHT self.contentView.frame.size.height
#define CWIDTH self.contentView.frame.size.width
static NSInteger t = 20;

@interface GameCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@end
@implementation GameCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self.contentView addSubview:_tableView];
    }
    [self dataHandeler];
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentInset = UIEdgeInsetsMake(94, 0, 59, 0);
    
    [_tableView registerClass:[HeaderTableViewCell class] forCellReuseIdentifier:@"headerCell"];
    
    [_tableView registerClass:[NormalTableViewCell class] forCellReuseIdentifier:@"normalCell"];
    
    [_tableView registerClass:[ThreePicturesTableViewCell class] forCellReuseIdentifier:@"imgextraCell"];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dataHandeler];
            [_tableView.mj_header endRefreshing];
        });
    }];
    // 设置刷新控件
    _tableView.mj_header = header;
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadingData];
        [_tableView.mj_footer endRefreshing];
    }];
    _tableView.mj_footer = footer;
    
}

- (void)dataHandeler
{
    NSString *urlStr = @"http://c.m.163.com/nc/article/list/T1348654151579/0-20.html";
    [LHAFNetworkTool getUrl:urlStr body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.dataArr = [NewsModel modelHanderWithArray:resposeObject[@"T1348654151579"]];
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    t = 20;
}

#pragma mark - 加载
- (void)loadingData
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348654151579/%ld-20.html", t];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *loadArr =[NewsModel modelHanderWithArray:resposeObject[@"T1348654151579"]];
        for (NSInteger i = 0; i < loadArr.count; i++) {
            [_dataArr addObject:loadArr[i]];
        }
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    t += 20;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *n = _dataArr[indexPath.row];
    if (indexPath.row == 0) {
        HeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
        [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.imgsrc]];
        cell.titleLabel.text = n.title;
        return cell;
    } else if (n.imgextra.count != 0) {
        ThreePicturesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imgextraCell" forIndexPath:indexPath];
        cell.titleLabel.text = n.title;
        [cell.leftImgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.imgsrc]];
        [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.imgextra[0][@"imgsrc"]]];
        [cell.rightImgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.imgextra[1][@"imgsrc"]]];
        return cell;
    } else {
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
        [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.imgsrc]];
        cell.titleLabel.text = n.title;
        cell.digestLabel.text = n.digest;
        // 截取时间
        NSString *beginStr = n.ptime;
        NSString *newbeginStr = [beginStr substringWithRange:NSMakeRange(5, 11)];
        cell.ptimeLabel.text = newbeginStr;
        NSString *str = [n.replyCount.stringValue stringByAppendingString:@"跟帖"];
        cell.replyCountLabel.text = str;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *n = _dataArr[indexPath.row];
    if (indexPath.row == 0) {
        return CHEIGHT / 3;
    } else if (n.imgextra.count != 0) {
        return CHEIGHT / 4;
    }
    else {
        return CHEIGHT / 6;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *n = _dataArr[indexPath.row];
    [self.delegate GameValue:n.url :n.title];
}
@end
